//
//  TTCoreUI.m
//  Tint
//
//  Created by Kevin Wu on 2/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTCoreUI.h"
#import "TTCoreCompatibility.h"
#import "TTDebug.h"


#pragma mark - Autoresizing mask

const NSUInteger TTViewAutoresizingKeepSize = 
UIViewAutoresizingFlexibleLeftMargin | 
UIViewAutoresizingFlexibleTopMargin | 
UIViewAutoresizingFlexibleRightMargin | 
UIViewAutoresizingFlexibleBottomMargin | 
!UIViewAutoresizingFlexibleWidth | 
!UIViewAutoresizingFlexibleHeight;

const NSUInteger TTViewAutoresizingKeepMargin = 
!UIViewAutoresizingFlexibleLeftMargin | 
!UIViewAutoresizingFlexibleTopMargin | 
!UIViewAutoresizingFlexibleRightMargin | 
!UIViewAutoresizingFlexibleBottomMargin | 
UIViewAutoresizingFlexibleWidth | 
UIViewAutoresizingFlexibleHeight;



#pragma mark - Animation duration constants

const CGFloat TTTransitionDurationFast = 0.2;
const CGFloat TTTransitionDurationNormal = 0.3;
const CGFloat TTTransitionDurationSlow = 0.4;
const CGFloat TTTransitionDurationFlip = 0.7;



#pragma mark - Dimensions constants

const CGFloat TTTableViewCellHeight = 44.0;

const CGFloat TTStatusBarHeight = 20.0;

const CGFloat TTToolbarHeightInPortrait = 44.0;
const CGFloat TTToolbarHeightInLandscape = 32.0;

const CGFloat TTNavigationBarHeightInPortrait = 44.0;
const CGFloat TTNavigationBarHeightInLandscape = 32.0;

const CGFloat TTKeyboardHeightInPortrait = 216.0;
const CGFloat TTKeyboardHeightInLandscape = 160.0;
const CGFloat TTPadKeyboardHeightInPortrait = 264.0;
const CGFloat TTPadKeyboardHeightInLandscape = 352.0;



#pragma mark - Orientation

UIDeviceOrientation TTDeviceOrientation(void) {
    return [UIDevice currentDevice].orientation;
}

BOOL TTIsDevicePortrait(void) {
    UIDeviceOrientation orient = TTDeviceOrientation();
    if (orient == UIDeviceOrientationPortrait || 
        orient == UIDeviceOrientationPortraitUpsideDown) {
        return YES;
    }
    return NO;
}

BOOL TTIsDeviceLandscape(void) {
    UIDeviceOrientation orient = TTDeviceOrientation();
    if (orient == UIDeviceOrientationLandscapeLeft || 
        orient == UIDeviceOrientationLandscapeRight) {
        return YES;
    }
    return NO;
}



UIInterfaceOrientation TTInterfaceOrientation(void) {
    return [UIApplication sharedApplication].statusBarOrientation;
}

BOOL TTIsInterfacePortrait(void) {
    UIInterfaceOrientation orient = TTInterfaceOrientation();
    if (orient == UIInterfaceOrientationPortrait || 
        orient == UIInterfaceOrientationPortraitUpsideDown) {
        return YES;
    }
    return NO;
}

BOOL TTIsInterfaceLandscape(void) {
    UIInterfaceOrientation orient = TTInterfaceOrientation();
    if (orient == UIInterfaceOrientationLandscapeLeft || 
        orient == UIInterfaceOrientationLandscapeRight) {
        return YES;
    }
    return NO;
}



BOOL TTIsSupportedOrientation(UIInterfaceOrientation orientation) {
    if (TTIsPad()) {
        return YES;
    }
    
    if (orientation == UIInterfaceOrientationPortrait || 
        orientation == UIInterfaceOrientationLandscapeLeft || 
        orientation == UIInterfaceOrientationLandscapeRight) {
        return YES;
    }
    
    return NO;
}



CGAffineTransform TTRotateTransformForOrientation(UIInterfaceOrientation orientation) {
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        return CGAffineTransformMakeRotation(M_PI*1.5);
        
    } else if (orientation == UIInterfaceOrientationLandscapeRight) {
        return CGAffineTransformMakeRotation(M_PI/2.0);
        
    } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
        return CGAffineTransformMakeRotation(-M_PI);
        
    }
    
    return CGAffineTransformIdentity;
}

CGAffineTransform TTSupportedRotateTransformForOrientation(UIInterfaceOrientation orientation) {
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        return CGAffineTransformMakeRotation(M_PI*1.5);
        
    } else if (orientation == UIInterfaceOrientationLandscapeRight) {
        return CGAffineTransformMakeRotation(M_PI/2.0);
        
    }
    
    return CGAffineTransformIdentity;
}



CGFloat TTToolbarHeightForOrientation(UIInterfaceOrientation orientation) {
    if (UIInterfaceOrientationIsPortrait(orientation) || TTIsPad()) {
        return TTToolbarHeightInPortrait;
    }
    return TTToolbarHeightInLandscape;
}

CGFloat TTNavigationBarHeightForOrientation(UIInterfaceOrientation orientation) {
    if (UIInterfaceOrientationIsPortrait(orientation) || TTIsPad()) {
        return TTNavigationBarHeightInPortrait;
    }
    return TTNavigationBarHeightInLandscape;
}

CGFloat TTKeyboardHeightForOrientation(UIInterfaceOrientation orientation) {
    if (TTIsPad()) {
        if (TTIsInterfacePortrait()) {
            return TTPadKeyboardHeightInPortrait;
        } else {
            return TTPadKeyboardHeightInLandscape;
        }
    } else {
        if (TTIsInterfacePortrait()) {
            return TTKeyboardHeightInPortrait;
        } else {
            return TTKeyboardHeightInLandscape;
        }
    }
    return 0.0;
}



#pragma mark - Keyboard

BOOL TTIsKeyboardVisible(void) {
    // Operates on the assumption that the keyboard is visible if and only if there is a first
    // responder; i.e. a control responding to key events
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *firstResponder = TTFindFirstResponderInView(window);
    return (firstResponder != nil);
}

UIView *TTFindFirstResponderInView(UIView *topView) {
    if ([topView isFirstResponder]) {
        return topView;
    }
    
    for (UIView *subview in topView.subviews) {
        if ([subview isFirstResponder]) {
            return subview;
        }
        
        UIView *firstResponder = TTFindFirstResponderInView(subview);
        if (nil != firstResponder) {
            return firstResponder;
        }
    }
    return nil;
}



#pragma mark - Debug

void TTDumpSubviews(UIView *view, NSInteger level) {
    NSMutableString *space = [NSMutableString string];
    for (int j=0; j<level; ++j)
        [space appendString:@"|_"];
    
    NSArray *subviews = view.subviews;
    for (int i=0; i<[subviews count]; ++i) {
        UIView *v = [subviews objectAtIndex:i];
        TTDPRINT(@"%@%d:%@ subviews:%d frame:%@", 
                 space, 
                 level, 
                 NSStringFromClass([v class]), 
                 [v.subviews count], 
                 NSStringFromCGRect(v.frame));
        TTDumpSubviews(v, level+1);
    }
}
